package com.hard;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lxk on 2019/7/7.
 */
public class Solution233 {
    Map<Integer,Integer> countMap = null;
    private int countOne(int num){
        int count = 0;
        while(num!=0){
            int posNum = num%10;
            num = num/10;
            if(countMap.containsKey(num)){
                return countMap.get(num);
            }
            if(posNum==1){
                count++;
            }
        }
        countMap.put(num,count);
        return count;
    }

    public int countDigitOne(int n) {
        countMap = new HashMap<>();
        int count = 0;
        for(int i = 1;i <= n; i++){
            count += countOne(i);
        }
        return count;
    }

}
